/* eslint-disable react/sort-comp */
import React, { Component, } from 'react';
import { StyleSheet, Button, View, ScrollView, } from 'react-native';
import BaseSelector from '../../app/selectors/base';
import * as BaseActions from '../../app/actions/base';
import SubmitSelector from '../../app/selectors/submit';
import AuthSelector from '../../app/selectors/auth';
import OpenAccountSelector from '../../app/selectors/openAccount';
import connect from '../../app/store/connect';
import AuthStepOne from './components/AuthStepOne.tsx';
import { GET, } from '../../commons/utils/request';
import urls from '../../app/constants/urls';
import Toast from '../../components/Toast';

const styles = StyleSheet.create({
  container:
   {
     flex: 1,
     display: 'flex',
     justifyContent: 'center',
     alignItems: 'center',
   },
  scrollview: {
    width: '100%',
    flex: 1,
  },
  nextStep: {
    marginTop: 50,
    height: 48,
    width: 320,
  },
});

@connect(BaseSelector,  BaseActions,)
@connect(SubmitSelector,  BaseActions,)
// @connect(AuthSelector,  BaseActions,)
export default class Account extends Component {
  static navigationOptions = {
    headerTitle: '首页',
  };

  constructor(p) {
    super(p);
    this.StepOne = null;
    this.state = {
      companyInfo: null,
    };
  }

  componentDidMount() {
    this.props.navigation.setParams({
      goBack: this.goBack,
    });
    this.queryCompanyInfo();
  }

  async queryCompanyInfo() {
    await GET(urls.companyInfo).then(resp => {
      console.log('TAGGG', `queryCompanyInfo====resp==${JSON.stringify(resp)}`);
      if (resp && resp.code === 0) {
        this.props.actions.auth({ authed: true, });
        this.setState({
          companyInfo: resp.data,
        });
      }
    });
  }

  goBack = (pageName) => {
    this.props.navigation.navigate(pageName);
  };

  onSubmit = () => {
    const _time = (new Date().getTime() - this.props.imgUploadTime || 0) / 1000;
    if (_time < 3) {
      Toast.show({ title: "图片上传中，请稍后再试", });
      return;
    }
    this.StepOne.submitData();
  };

  onAuthCallback = (authed) => {
    console.log('TAGGG', `onAuthCallback====authed==${authed}`);
    if (authed) {
      this.props.actions.auth({ authed: true, });
    }
  };

  render() {
    return (
      <View
        style={styles.container}
        onStartShouldSetResponderCapture={(e) => {
          if (this.state.companyInfo) {
            return true;
          }
          return false;
        }}
      >
        <ScrollView style={styles.scrollview}>
          <AuthStepOne
            ref={ref => {
              this.StepOne = ref;
            }}
            companyInfo={this.state.companyInfo}
            onPress={this.onAuthCallback}
            navigation={this.props.navigation}
          >
            注册
          </AuthStepOne>
        </ScrollView>
        {
          this.state.companyInfo ? null : (
            <View style={styles.nextStep}>
              <Button
                title="提交"
                onPress={this.onSubmit}
              />
            </View>
          )
        }
      </View>
    );
  }
}
